"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 服务端工具
Case Name   : gs_dump和pg_get_tabledef()导出自生成列定义
Create At   : 2022/7/29
Owner       : @daiguatutu
Description :
    1.创建表
    create table test(a int,b int generated always as (length(a::text)) stored);
    2.使用pg_get_tabledef()导出自生成列定义
    select pg_get_tabledef(a.oid) from pg_class a where relname='test';
    3.使用gs_dump导出自生成列定义
    gs_dump -U 用户 -p 端口 postgres -t test
Expect      :
    1.创建表成功
    2.pg_get_tabledef()导出成功
    3.3.gs_dump导出成功
History     :
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.constant = Constant()
        self.pri_user = Node('PrimaryDbUser')
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.t_name = "t_dump_0131"

    def test_tools_dump(self):
        text = '-----step1:创建表; expect:创建成功---'
        self.log.info(text)
        sql_cmd = f'drop table if exists {self.t_name};' \
            f'create table {self.t_name}(a int,' \
            f'b int generated always as (length(a::text)) stored);'
        sql_result = self.sh_primary.execut_db_sql(sql=sql_cmd)
        self.log.info(sql_result)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, sql_result,
                      '执行失败:' + text)

        text = '-----step2:使用pg_get_tabledef()导出自生成列定义; expect:导出成功-----'
        self.log.info(text)
        sql_cmd = f"select pg_get_tabledef(a.oid) from pg_class a " \
            f"where relname='{self.t_name}';"
        sql_result = self.sh_primary.execut_db_sql(sql=sql_cmd)
        self.log.info(sql_result)
        expect = f'b integer GENERATED ALWAYS AS (length((a)::text)) STORED'
        self.assertIn(expect, sql_result, '执行失败:' + text)

        text = '-----step3:使用gs_dump导出自生成列定义; expect:导出成功-----'
        self.log.info(text)
        dump_cmd = f'source {macro.DB_ENV_PATH};' \
            f'gs_dump -U {self.pri_user.ssh_user} ' \
            f'-p {self.pri_user.db_port} ' \
            f'{self.pri_user.db_name} ' \
            f'-t {self.t_name};'
        self.log.info(dump_cmd)
        dump_result = self.pri_user.sh(dump_cmd).result()
        self.log.info(dump_result)
        self.assertIn(expect, dump_result, '执行失败:' + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text = '----删除表; expect:成功----'
        self.log.info(text)
        sql_cmd = f'drop table if exists {self.t_name};'
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, sql_res,
                      '执行失败:' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
